{{/* Sets page to either the given frontmatter section variable, current
  parent section or if the current page is a section, the current page */}}
{{- $page := cond (ne .Params.section nil) .Params.section  .root.Section -}}
{{/* Weird Hugo hack coming up. Homepage and some sections' pages are fetched
  with different types! */}}
{{- .page_scratch.Set "page" ((.Site.GetPage "Section" $page) | default (.Site.GetPage "section" $page)) -}}
{{- if .root.CurrentSection -}}
  {{- if and (not .Params.section) (eq .root.CurrentSection.Kind "section") -}}
    {{- .page_scratch.Set "page" .root.CurrentSection -}}
  {{- end -}}
{{- end -}}

{{- .page_scratch.Set "pages" slice -}}
{{- range (.page_scratch.Get "page").Pages -}}
  {{- if or $.Params.subsections (ne .Kind "section") -}}
    {{- $.page_scratch.Add "pages" . -}}
  {{- end -}}
{{- end -}}
{{- if .Params.subsection_leaves -}}
  {{- range (.page_scratch.Get "page").Sections -}}
    {{- $.page_scratch.Add "pages" .Pages -}}
  {{- end -}}
{{- end -}}

{{/* Pagination is used if paginate (default value is 10) is more than
  frontmatter count variable */}}
{{- $renderPagination := and (in (slice "section" "home") .root.Kind) (or (not (isset .Params "count")) (le .Params.count 0)) -}}
{{- if $renderPagination -}}
  {{- $paginator := .root.Paginate ($.page_scratch.Get "pages") -}}
  {{- .page_scratch.Set "pages" $paginator.Pages -}}
  {{- .page_scratch.Set "paginator" $paginator -}}
{{- end -}}

{{- $filtered_pages := (partial "helpers/filter-special-pages.html" (dict "pages" (.page_scratch.Get "pages") "page_scratch" .page_scratch)) -}}
{{- $sorted_pages := (sort $filtered_pages (.Params.sort | default "PublishDate") (.Params.sort_order | default "desc")) -}}

{{- return (dict "sorted_pages" $sorted_pages "renderPagination" $renderPagination) -}}
